package org.kernely.scheduling;

import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.kernely.core.service.KernelyBundleService;
import org.kernely.scheduling.administration.SchedulingAdminPresenter;
import org.kernely.scheduling.administration.SchedulingAdminView;
import org.kernely.scheduling.service.KernelySchedulingService;
import org.kernely.security.service.KernelySecurityService;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventBus;
import org.kernely.ui.mvp.KernelyViewPresenterManager;
import org.kernely.ui.mvp.ViewIdentifier;
import org.kernely.ui.services.KernelyUIBundle;
import org.kernely.ui.services.KernelyUIBundleService;
import org.kernely.ui.utils.KernelyBundleMenu;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;

public class SchedulingBundle implements KernelyUIBundle {

	private static KernelyUIBundleService uiBundleService;
	private KernelySecurityService securityService;
	private static KernelySchedulingService schedulingService;

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getAdministrationView()
	 */
	public Class<? extends IKernelyView> getAdministrationView() {
		return SchedulingAdminView.class;
	}

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getFrontPages()
	 */
	public Set<ViewIdentifier> getFrontPages() {
		return null;
	}

	/**
	 * @see org.kernely.ui.mvp.KernelyUIBundle#getIcon()
	 */
	public final Resource getIcon() {
		return new ExternalResource("schedulingbundle/images/scheduling_bundle_32.png");
	}

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getMenu()
	 */
	public KernelyBundleMenu getMenu() {
		return null;
	}

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getViewPresenter()
	 */
	public KernelyViewPresenterManager getViewPresenter() {
		KernelyViewPresenterManager manager = new KernelyViewPresenterManager();
		manager.put(SchedulingAdminView.class, SchedulingAdminPresenter.class);
		return manager;
	}

	/**
	 * @see org.kernely.core.service.KernelyBundle#getName()
	 */
	public String getName() {
		return "Scheduling bundle";
	}

	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to register this bundle
	 */
	public final void setModuleService(KernelyBundleService service) {
		// Registering the bundle
		service.registerModule(this);
	}

	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to unregister this bundle
	 */
	public final void unsetModuleService(KernelyBundleService service) {
		// Unregistering the bundle
		service.unregisterModule(this);
	}

	/**
	 * Sets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void setUIBundleService(KernelyUIBundleService uiBundleService) {
		SchedulingBundle.uiBundleService = uiBundleService;
		SchedulingBundle.uiBundleService.registerModule(this);
	}

	/**
	 * Unsets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void unsetUIBundleService(KernelyUIBundleService service) {
		SchedulingBundle.uiBundleService.unregisterModule(this);
	}
	
	/**
	 * Sets the security Declarative Service
	 * @param service The security service.
	 */
	public final void setSecurityService(KernelySecurityService service) {
		securityService = service;
	}

	/**
	 * Unsets the security Declarative Service
	 * @param service The security service.
	 */
	public final void unsetSecurityService(KernelySecurityService service) {}

	/**
	 * Set the scheduling service, managed by Declarative Service.
	 * @param service the scheduling service.
	 */
	public final void setSchedulingService(KernelySchedulingService service) {
		schedulingService = service;
	}

	/**
	 * Get the scheduling service to manage tasks.
	 */
	public final static KernelySchedulingService getSchedulingService(){
		return schedulingService;
		
	}
	
	/**
	 * Unset the scheduling service, managed by Declarative Service.
	 * @param the service.
	 */
	public final void unsetSchedulingService(KernelySchedulingService service) {}

	/**
	 * Get the user's event bus
	 * @return the event bus, usefull to send UI event (to refresh for example).
	 */
	public static KernelyEventBus getEventBus() {
		Session session = (Session) SecurityUtils.getSubject().getSession();
		return (KernelyEventBus) session.getAttribute("eventBus");
	}
	
	
}
